package com.lujianfei.switchtheme.utils

import android.os.Environment
import android.util.Log
import com.lujianfei.switchtheme.Constant
import com.lujianfei.switchtheme.MyApplication
import java.io.File
import java.io.FileOutputStream

object FileHelper {
    
    const val TAG = "FileHelper"
    
    fun getSDcardDir() = Environment.getExternalStorageDirectory().absoluteFile.absolutePath

    fun copySkinToDataDir() {
        copySkinFrom(Constant.Skin.SKIN1)
    }

    private fun copySkinFrom(fileName:String) {
        val context = MyApplication.INSTANCE
        val file = File(context.filesDir, fileName)
        Log.d(TAG, "copySkinToDataDir  ${file.absolutePath}")
        if (file.exists()) return

        Log.d(TAG, "start copying skin ...")
        val fileOutputStream = FileOutputStream(file)
        fileOutputStream.use { fileOutputStream ->
            context.assets.open("skins/$fileName").use { inputStream ->
                val buffer = ByteArray(1024)
                while (true) {
                    val c = inputStream.read(buffer)
                    if (c < 0) break
                    fileOutputStream.write(buffer, 0, c)
                }
                fileOutputStream.flush()
            }
        }
        Log.d(TAG, "Skin copied at ${file.absolutePath}")
    }
}